<?
require_once('config.php');
$action = getValue('action','str','POST');
if($action == 'submit'){
   $respone = array();
   $array_videos = array();
   $link_youtube = getValue('link_youtube','str','POST');
   $strpos = strpos($link_youtube,'watch?v=');
   if($strpos){
      $vid_id = substr($link_youtube,$strpos+8);      
   }else{
      $vid_id = $link_youtube;
   }
   $cat_id   = getValue('cat_id','int','POST');
   $pla_id   = getValue('pla_id','int','POST');
   $vid_app  = getValue('vid_app','int','POST');
   $vid_type = getValue('vid_type','int','POST',0);
   $link_api = "https://gdata.youtube.com/feeds/api/videos/".$vid_id."?v=2&alt=json";
   
   /*-------- Kiểm tra respone header từ link api ---------*/
   if(get_http_response_code($link_api) != 200){
      echo 'ID hoặc link video không tồn tại. Vui lòng kiểm tra lại !';
      exit();
   }
   
   /*--------- Get content from link API youtube ----------*/
   $respone = json_decode(file_get_contents($link_api),true);
   if(count($respone) >0){
      
      $array_videos = $respone['entry'];
   }
   if(count($array_videos) > 0){
      $value_insert = '';
      $sql_insert   = '';
      $array_media_content    = $array_videos['media$group']['media$content'];
      $array_media_thumbnail  = $array_videos['media$group']['media$thumbnail'];
      $vid_id_youtube = $vid_id;
      $db_count = new db_count($sql = "SELECT vid_id FROM video WHERE vid_id_youtube='" . $vid_id_youtube."'");
      if($db_count->total == 0){
         $vid_name  = $array_videos['title']['$t'];
         $vid_lyric = '';
         $vid_duration = formatDuraction($array_media_content[0]['duration']);
         $vid_image    = handleNameImage($array_media_thumbnail[1]['url']);
         $value_insert .= "('" . $vid_name . "',
                            " . $cat_id . ",
                            " . $pla_id . ",
                            " . $vid_app . ",
                            '" . $vid_duration . "',
                            '" . $vid_image . "',
                            " . time() . ",
                            '" . $vid_id_youtube . "',
                            " . $vid_type . "),";
            
         if($value_insert != ''){
            $value_insert = substr($value_insert,0,-1); 
            $sql_insert = "INSERT INTO 
                           video(vid_name,
                                 cat_id,
                                 pla_id,
                                 vid_app,
                                 vid_duration,
                                 vid_image,
                                 vid_date,
                                 vid_id_youtube,
                                 vid_type)
                           VALUES
                              " . $value_insert;  
         }
         if($sql_insert != ''){
            $db_exe = new db_execute($sql_insert);
            $rows_inserted = $db_exe->rows_affected;
         }
         
         
         echo '<span style="color: #01B42C;font-size: 14px;font-weight: bold;">Thêm thành công '.$rows_inserted.' videos </span>';   
         }else{
            echo '<span style="color: red;font-size: 14px;font-weight: bold;">Đã tồn tại video này</span>';
         }
      }
   
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thêm mới <?=$moduol_name?></title>
   <?=$css_global?>
   <?=$js_global?>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker();
    $(document).on('click','.reload_tab' ,function(){
      window.location.reload();
    });
  });
  </script>
</head>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Thêm mới <?=$moduol_name?></h1>      
      <div class="reload_tab" title="Reload"></div>                         
      <div class="clear"></div>
   </div>
   <div class="block-fluid">
      <form class="form-horizontal" name="add" action="" method="POST" enctype="multipart/form-data"  onsubmit="">
         <input type="hidden" name="action" value="submit" />
         <fieldset>
            <div class="control-group">
               <label class="control-label" for="input01">Link youtube hoặc ID</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span5" id="link_youtube" name="link_youtube" title="Tên channel"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Loại nhạc</label>
               <div class="controls">
                  <select id="vid_type" name="vid_type">
                     <option value="0">Không lời</option>
                     <option value="1">Có lời</option>
                  </select>
               </div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn thể loại</label>
               <div class="controls">
						<select id="cat_id" name="cat_id">
                     <option value="0">Không chọn</option>
                     <?if($count_cat > 0):?>
                        <?foreach($array_all_cat as $key=>$value):?>
                           <option value="<?=$key?>"><?=$value?></option>
                        <?endforeach?>
                     <?endif?>
                  </select>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn playlist</label>
               <div class="controls">
               <?php if(count($arr_pla) > 0){?>
                  <select id="pla_id" name="pla_id">
                     <option value="0">Không chọn</option>
                        <?foreach($arr_pla as $value):?>
                           <option value="<?=$value['pla_id']?>"><?=$value['pla_name']?></option>
                        <?endforeach?>
                  </select>
               <?php }?>
               </div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Chọn App</label>
               <div class="controls">
               <?php if(count($arry_app) > 0){?>
                  <select id="vid_app" name="vid_app">
                        <?foreach($arry_app as $key=>$value):?>
                           <option value="<?=$key?>"><?=$value?></option>
                        <?endforeach?>
                  </select>
               <?php }?>
               </div>
            </div>
            <div class="form-actions">
					<button type="submit" class="btn btn-info">Thêm</button>
					<button type="reset"  class="btn btn-warning">Làm lại</button>
				</div>
         </fieldset>
      </form>
   </div> 
</div>